package com.buba.service.impl;

import com.buba.mapper.*;
import com.buba.service.FollowUserService;
import entity.FollowTopicEntity;
import entity.FollowUserEntiry;
import entity.KudosEntity;
import entity.VO.DailyRecordVO;
import entity.VO.ReleaseTopicVO;
import entity.VO.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 孔德嘉
 * @Description: TODO关注的人ServiceImpl
 * @DateTime: 2023/3/6 19:48
 **/
@Service
public class FollowUserServiceImpl implements FollowUserService {
    //注入
    @Autowired
    private FollowUserMapper followUserMapper;
    //注入
    @Autowired
    private DailyRecordMapper dailyRecordMapper;
    //注入
    @Autowired
    private FileMapper fileMapper;
    //注入
    @Autowired
    private TopicMapper topicMapper;
    //注入
    @Autowired
    private KudosMapper kudosMapper;
    /*
     * @param id
     * @return result.CommonResult
     * @author kdj
     * @describe:查询关注人的发布的日记话题
     * @date 2023/3/6 19:46
     */
    @Override
    public List<Map> getFollowUserIndexList(int id) {
        //定义一个List用于存放内容
        List<Map> list=new ArrayList<>();
        //获取用户关注的人的id,use_name,user_file
        List<UserVO> followUserInformation = followUserMapper.getFollowUserInformation(id);
        //遍历followUserInformation
        for (UserVO userVO : followUserInformation) {
            //取出关注的用户id 查询到此用户发布的日记
            List<DailyRecordVO> dailyRecordByUserId = dailyRecordMapper.getDailyRecordByUserId(userVO.getUserId());
            //取出关注的用户id 查询到此用户发布的话题
            List<ReleaseTopicVO> topicByUserId = topicMapper.getTopicByUserId(userVO.getUserId());
            //根据dailyRecordByUserId查询到点赞数 评论数
            for (DailyRecordVO dailyRecordVO : dailyRecordByUserId) {
                //将日记话题图片id分割转换成数组
                String[] contentFileId = dailyRecordVO.getDiaryRecordFileId().split(",");
                //查日记。话题图片
                List<String> contentFileIds = fileMapper.getDiaryRecordFileId(contentFileId);
                //查询到评论数
                String commentNum = dailyRecordMapper.getCommentNum(dailyRecordVO.getDailyRecordId(), "1");
                //查询到点赞数
                int kudosNum = kudosMapper.getKudosNum(dailyRecordVO.getDailyRecordId(), 1);
                //获取点赞列表
                List<UserVO> kudosUserList = kudosMapper.getKudosUserList(dailyRecordVO.getDailyRecordId(), 1,false);
                //定义点赞头像列表
                ArrayList<String> kudosUserFileList = new ArrayList<>(kudosUserList.size());
                //查询该用户是否点赞过
                KudosEntity kudosById = kudosMapper.getKudosById(dailyRecordVO.getDailyRecordId(), 1, userVO.getUserId());
                //定义变量 判断是否点赞
                Boolean isKudosByUserId;
                //判断KudosEntity是否为空，kudos_whether是否为0
                if(kudosById==null || kudosById.getKudosWhether()==1){
                    //没点赞
                    isKudosByUserId=false;
                }else{
                    //点赞了
                    isKudosByUserId=true;
                }
                //获取用户头像
                for (UserVO vo : kudosUserList) {
                    //添加到数组里面
                    kudosUserFileList.add(vo.getUserFilePath());
                }
                //创建Map
                Map<String, Object> map = new HashMap<>();
                //存入到map当中
                map.put("type",1);//日记为1 话题为2
                map.put("userName",userVO.getUserName());//用户名称
                map.put("userFilePath",userVO.getUserFilePath());//用户头像
                map.put("id",dailyRecordVO.getDailyRecordId());//内容id
                map.put("contentFilePath",contentFileIds.get(0));//内容图片（纸需要第一个）
                map.put("caption",dailyRecordVO.getDiaryCaption());//标题
                map.put("content",dailyRecordVO.getDiaryRecordContent());//内容
                map.put("browse",dailyRecordVO.getDiaryRecordBrowse());//浏览
                map.put("labelName",dailyRecordVO.getDiaryItemLabelName());//标签
                map.put("commentNum",commentNum);//评论数
                map.put("kudosNum",kudosNum);//点赞数
                map.put("createTime",dailyRecordVO.getCreateTime());//点赞数
                map.put("kudosUserFileList",kudosUserFileList);//点赞列表
                map.put("isKudosByUserId",isKudosByUserId);//是否点赞
                //将map添加到list当中
                list.add(map);
            }
            //根据dtopicByUserId查询到点赞数 评论数
            for (ReleaseTopicVO releaseTopic : topicByUserId) {
                //将日记话题图片id分割转换成数组
                String[] contentFileId = releaseTopic.getReleaseTopicFileId().split(",");
                //查日记。话题图片
                List<String> contentFileIds = fileMapper.getDiaryRecordFileId(contentFileId);
                //查询到评论数
                String commentNum = dailyRecordMapper.getCommentNum(releaseTopic.getReleaseTopicId(), "2");
                //查询到点赞数
                int kudosNum = kudosMapper.getKudosNum(releaseTopic.getReleaseTopicId(), 2);
                //获取点赞列表
                List<UserVO> kudosUserList = kudosMapper.getKudosUserList(releaseTopic.getReleaseTopicId(), 2,false);
                //查询该用户是否点赞过
                KudosEntity kudosById = kudosMapper.getKudosById(releaseTopic.getReleaseTopicId(), 2, userVO.getUserId());
                //定义变量 判断是否点赞
                Boolean isKudosByUserId;
                //判断KudosEntity是否为空，kudos_whether是否为0
                if(kudosById==null || kudosById.getKudosWhether()==1){
                    //没点赞
                    isKudosByUserId=false;
                }else{
                    //点赞了
                    isKudosByUserId=true;
                }
                //定义点赞头像列表
                ArrayList<String> kudosUserFileList = new ArrayList<>(kudosUserList.size());
                //获取用户头像
                for (UserVO vo : kudosUserList) {
                    //添加到数组里面
                    kudosUserFileList.add(vo.getUserFilePath());
                }
                //创建Map
                Map<String, Object> map = new HashMap<>();
                //存入到map当中
                map.put("type",2);//日记为1 话题为2
                map.put("userName",userVO.getUserName());//用户名称
                map.put("userFilePath",userVO.getUserFilePath());//用户头像
                map.put("id",releaseTopic.getReleaseTopicId());//内容id
                map.put("contentFilePath",contentFileIds.get(0));//内容图片（纸需要第一个）
                map.put("caption",releaseTopic.getTopicName());//标题
                map.put("content",releaseTopic.getReleaseTopicContent());//内容
                map.put("browse",releaseTopic.getDiaryRecordBrowse());//浏览
                map.put("labelName",releaseTopic.getTopicName());//标签
                map.put("commentNum",commentNum);//评论数
                map.put("kudosNum",kudosNum);//点赞数
                map.put("createTime",releaseTopic.getCreateTime());//点赞数
                map.put("kudosUserFileList",kudosUserFileList);//点赞列表
                map.put("isKudosByUserId",isKudosByUserId);//是否点赞
                //将map添加到list当中
                list.add(map);
            }
        }
        //对list排序

        // 使用Stream API按照createtime属性排序
        List<Map> sortedList = list.stream()
                .sorted(Comparator.comparing(m -> (Date) m.get("createTime"),Comparator.reverseOrder()))
                .collect(Collectors.toList());


        return sortedList;
    }

    /*
     * @param coverUserId 被关注人id
     * @param userID 关主人id
     * @return int
     * @author kdj
     * @describe:
     * @date 2023/3/10 9:47
     */
    @Override
    public int updateFollowUserByUserId(int coverUserId, int userId) {
        int i =0;//定义i判断是否修改状态成功
        //查询话题有没有被该用户关注
        FollowUserEntiry followByUserId = followUserMapper.getFollowByUserId(coverUserId, userId);
        //判断数据库中是否有  关注过的记录
        if(followByUserId!=null || followByUserId.getDelFlag()==1){
            //如果数据库中有数据 被关注 del_flag改为1
            if(followByUserId.getDelFlag()==0){
                followByUserId.setDelFlag(1);
                i = followUserMapper.upFollowStatus(followByUserId);
            }else{
                //如果数据库中有数据   del_flag改为0
                followByUserId.setDelFlag(0);
                i = followUserMapper.upFollowStatus(followByUserId);
            }
        }else{
            followByUserId=new FollowUserEntiry();
            followByUserId.setFollowUserCoverUserId(coverUserId);
            followByUserId.setFollowUserUserId(userId);
            //如果数据库中没数据  添加数据
            i = followUserMapper.addFollowUser(followByUserId);
        }
        return i;
    }

}

